package com.sheeran.aichat.controller.bigmodel;

import com.sheeran.aichat.dto.bigmodel.*;
import com.sheeran.aichat.dto.common.rest.WrappedResponse;
import com.sheeran.aichat.service.bigmodel.impl.ChatGLM;
import com.sheeran.aichat.utils.rest.ResponseCode;
import com.sheeran.aichat.utils.rest.WrappedResponseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.Collections;

/**
 * @author: sheeran
 * @create: 2025/02/11
 */
@Api(tags = "智谱大模型")
@RequiredArgsConstructor
@RestController
@WrappedResponseController
@RequestMapping("/big-model/chat")
public class ChatGLMController {

    private final ChatGLM chatGLM;

    @ApiOperation("智谱大模型GLM-4系列调用API")
    @PostMapping("/completions")
    public BigModelResponseDTO completions (@RequestBody @Valid ChatCompletionRequestDTO request) {
        return chatGLM.completions(request);
    }

    @ApiOperation("智谱大模型GLM-4系列调用API")
    @GetMapping("/completions/{content}")
    public String completions2 (@PathVariable("content") String content) {
        ChatCompletionRequestDTO request = new ChatCompletionRequestDTO();
        messagesRequestDTO prompt = new messagesRequestDTO();
        prompt.setRole("user");
        prompt.setContent(content);
        request.setMessages(Collections.singletonList(prompt));
        BigModelResponseDTO completions = chatGLM.completions(request);
        return completions.getChoices().get(0).getMessage().getContent();
    }

    @ApiOperation("Web-Search-Pro")
    @PostMapping("/tools")
    public BigModelToolsResponseDTO tools (@RequestBody @Valid ChatToolsRequestDTO request) {
        return chatGLM.tools(request);
    }

    @ApiOperation("Web-Search-Pro")
    @GetMapping("/tools/{content}")
    public Object tools2 (@PathVariable("content") String content) {
        ChatToolsRequestDTO request = new ChatToolsRequestDTO();
        messagesRequestDTO prompt = new messagesRequestDTO();
        request.setTool("web-search-pro");
        prompt.setRole("user");
        prompt.setContent(content);
        request.setMessages(Collections.singletonList(prompt));
        BigModelToolsResponseDTO tools = chatGLM.tools(request);
        return tools.getChoices().get(0).getMessage().getToolCalls();
    }
}
